import { EditCompanies } from "@/components/form/companies/edit.companies"
import { FormModal } from "@/components/modal/form.modal"
import { Field_Search } from "@/components/utils/field.list"
import companiesService from "@/services/companies.service"
import { StoreContext } from "@/store"
import { AsyncModal, OperateRender, IndexRender, FormRender, ListTable, DateRender } from "@whzhuke/antd"
import { useMobx } from "@whzhuke/mobx"
import { Button, Col, Modal, Row, Space } from "antd"
import { useForm } from "antd/es/form/Form"
import React, { useCallback, useContext } from "react"
const Page = useMobx(() => {
  const [form] = useForm()
  const { ListStore } = useContext(StoreContext)
  const titleRender = useCallback(() => {
    return (
      <Row justify="space-between" align="middle">
        <Col> </Col>
        <Col>
          <Space size="middle">
            <Button type="primary" onClick={AddModal()}>
              添加
            </Button>
            <Button type="primary" onClick={Zanwu()}>
              导出
            </Button>
          </Space>
        </Col>
      </Row>
    )
  }, [])
  const Zanwu = useCallback(() => async () => {}, [])
  const AddModal = useCallback(
    (value?: any) => async () => {
      const done = await AsyncModal({
        content: FormModal,
        params: {
          formProps: {
            value: { ...value }
          },
          formEle: EditCompanies,
          title: "添加供应商"
        }
      })
      if (done) {
        ListStore.updateList(companiesService.list)
      }
    },
    []
  )

  const onFinish = useCallback(data => {
    const [start, end] = data.range || []
    data.startTime = start ? new Date(start).getTime() : undefined
    data.endTime = end ? new Date(end).getTime() : undefined
    delete data.range
    ListStore.setParams(companiesService.list, data)
  }, [])
  const EditModal = useCallback(
    (val?, item?) => async () => {
      const done = await AsyncModal({
        content: FormModal,
        params: {
          formEle: EditCompanies,
          formProps: {
            values: { ...item }
          },
          title: item ? "修改供应商" : "添加供应商"
        }
      })
      if (done) {
        ListStore.updateList(companiesService.list)
      }
    },
    []
  )

  const companyTypeRender = useCallback((val: number | string) => {
    const map: Record<number | string, string> = {
      1: "供应商",
      2: "国外客户"
    }
    return map[val] || "-"
  }, [])

  return (
    <Row>
      <Col span={24}>
        <FormRender
          form={form}
          onFinish={onFinish}
          layout="inline"
          fieldList={[
            {
              fieldType: "rangeDate",
              formItemConfig: {
                name: "range",
                label: "创建时间"
              },
              config: {
                showTime: !0,
                format: "YYYY-MM-DD HH:mm:ss"
              }
            },
            {
              fieldType: "input",
              formItemConfig: {
                name: "keyword"
              },
              config: {
                placeholder: "关键字搜索"
              }
            },

            {
              fieldType: "select",
              formItemConfig: {
                name: "type",
                label: "公司性质"
              },
              config: {
                placeholder: "请选择公司性质",
                options: [
                  {
                    label: "供应商",
                    value: 1
                  },
                  {
                    label: "国外客户",
                    value: 2
                  }
                ]
              }
            },

            Field_Search
          ]}
        />
      </Col>
      <Col span={24}>
        <ListTable
          title={titleRender}
          apiColumns={[
            IndexRender("id", 80),
            ["name", "供应商名称"],
            ["address", "地址"],
            ["contacts", "联系人"],
            ["username", "账号"],
            ["type", "公司性质", companyTypeRender],
            ["belongBank", "开户行"],
            ["account", "银行账号"],
            ["taxpayerCode", "税号"],
            ["createTs", "创建时间", DateRender()],
            ["id", "操作", OperateRender([["修改", EditModal]]), { width: 80, fixed: "right" }]
          ]}
          apiService={companiesService.list}
        />
      </Col>
    </Row>
  )
})

export default Page
